package com.xiaoyu.client.adapter.seek;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xiaoyu.client.R;
import com.xiaoyu.client.adapter.ImageAdapter;
import com.xiaoyu.client.model.seek.SeekParam;
import com.xiaoyu.commonlib.ui.OnItemRecyClickListener;
import com.xiaoyu.commonlib.ui.widget.NestedGridView;
import com.xiaoyu.commonlib.utils.CCircleImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * 求助列表适配器
 * Created by jk on 2018/10/11.
 */
public class HelpRecyAdapter extends RecyclerView.Adapter<HelpRecyAdapter.ViewHolder> {
    private Context mContext;
    private List<SeekParam> mList = new ArrayList<>();
    private OnItemRecyClickListener mListener;

    public HelpRecyAdapter(Context context, List<SeekParam> list) {
        this.mContext = context;
        this.mList = list;
    }

    @NonNull
    @Override
    public HelpRecyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_help, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final HelpRecyAdapter.ViewHolder holder, final int position) {
        Glide.with(mContext).load(mList.get(position).getUserphoto()).into(holder.portrait);
        holder.name.setText(mList.get(position).getNickname());
        holder.time.setText(mList.get(position).getSeektime() + "发布");
        holder.content.setText(mList.get(position).getSeektitle());
        if (Integer.parseInt(mList.get(position).getSeekstate()) == 0) {//未解决
            holder.status.setImageResource(R.mipmap.icon_unsolved);
        } else {
            holder.status.setImageResource(R.mipmap.icon_solved);
        }
        holder.helpNum.setText("以帮助 " + mList.get(position).getSeeksum());
        holder.type.setText(mList.get(position).getSeephelpname());
        holder.locate.setText(mList.get(position).getSeekress());

        if (mList.get(position).getSeekimg().size() > 0) {
            holder.mGridView.setVisibility(View.VISIBLE);
        }else {
            holder.mGridView.setVisibility(View.GONE);
        }

        ImageAdapter mAdapter = new ImageAdapter(mContext,mList.get(position).getSeekimg());
        holder.mGridView.setAdapter(mAdapter);

        holder.mGridView.setClickable(false);
        holder.mGridView.setPressed(false);
        holder.mGridView.setEnabled(false);


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onItemRecyClick(holder.itemView, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private CCircleImageView portrait;
        private TextView name;
        private TextView time;
        private TextView content;
        private TextView helpNum;
        private TextView type;
        private TextView locate;
        private ImageView status;
        private NestedGridView mGridView;

        public ViewHolder(View itemView) {
            super(itemView);
            portrait = itemView.findViewById(R.id.item_help_portrait);
            name = itemView.findViewById(R.id.item_help_helper_name_txt);
            time = itemView.findViewById(R.id.item_help_publish_time_txt);
            content = itemView.findViewById(R.id.item_help_content_txt);
            helpNum = itemView.findViewById(R.id.item_help_solved_num_txt);
            type = itemView.findViewById(R.id.item_help_type_label_txt);
            locate = itemView.findViewById(R.id.item_help_locate_txt);
            status = itemView.findViewById(R.id.item_help_solve_status_img);
            mGridView = itemView.findViewById(R.id.item_help_img_list_container);
        }
    }

    public void setOnItemRecyClickListener(OnItemRecyClickListener mListener) {
        this.mListener = mListener;
    }

    public void updateList(List<SeekParam> list){
        this.mList = list;
        this.notifyDataSetChanged();
    }
}
